package cn.dnui_yxk801.dnuifood_16110100801.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.dnui_yxk801.dnuifood_16110100801.R;
import cn.dnui_yxk801.dnuifood_16110100801.bean.FoodBean;
import cn.dnui_yxk801.dnuifood_16110100801.bean.ShopBean;
import cn.dnui_yxk801.dnuifood_16110100801.controller.FoodInfoActivity;
import cn.dnui_yxk801.dnuifood_16110100801.listener.OnRetrofitListener;
import cn.dnui_yxk801.dnuifood_16110100801.model.ShopModel;

public class FoodListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>implements OnRetrofitListener{
    private Context context;
    private String shopname;
    private String shopid;
    private List<FoodBean> datalist;
    private LayoutInflater layoutInflater;
    private static String Baseurl="http://172.24.10.175:8080/foodService/";


    public FoodListAdapter(Context context,List datalist)
    {
        this.context=context;
        this.datalist=datalist;
        layoutInflater=LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v=layoutInflater.from(viewGroup.getContext()).inflate(R.layout.fooditem,viewGroup,false);
        return new FoodListAdapter.ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int i) {
        FoodBean entity=(FoodBean)datalist.get(i);
        if (entity==null)
            return;
        FoodListAdapter.ViewHolder viewHolder=(FoodListAdapter.ViewHolder)holder;
        shopid=entity.getShop_id();
        ShopModel shopModel=new ShopModel();
        shopModel.getShopById(shopid,this);

        viewHolder.foodname.setText(entity.getFoodname());
        viewHolder.price.setText(entity.getPrice());
        Picasso.with(context).load(Baseurl+entity.getPic()).into(viewHolder.food_pic);
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //Toast.makeText(context,"",Toast.LENGTH_SHORT).show();
                Intent intent=new Intent( context,FoodInfoActivity.class);
                String shop_id=datalist.get(i).getShop_id();
                String food_id=datalist.get(i).getFood_id();
                intent.putExtra("shop_id",shop_id);
                intent.putExtra("food_id",food_id);
                intent.putExtra("shop_name",shopname);
                context.startActivity(intent);
            }
        });


    }

    @Override
    public int getItemCount() {
        return datalist.size();
    }

    @Override
    public void onSuccess(Object object, int flag) {
        if (flag==9){
            ShopBean shopBean=(ShopBean)object;
            shopname=shopBean.getShopname();
        }
    }

    @Override
    public void onFalure(String msg) {

    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView foodname, price;
        ImageView food_pic;


        public ViewHolder(View view) {
            super(view);
            foodname = (TextView) view.findViewById(R.id.foodname);
            price = (TextView) view.findViewById(R.id.price);
            food_pic = (ImageView) view.findViewById(R.id.foodimage);

        }
    }
}
